#!/usr/bin/env bash
echo "input sudo password" && read SUDO_PASSWD
function modify_repo() {
    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

    cat <<"EOF" | sudo tee /etc/apt/sources.list
# 默认注释了源码镜像以提高 apt update 速度，如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

# 预发布软件源，不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
EOF
}

function install_emacs() {
    (sudo apt-get update -y && sudo apt-get install emacs -y) || return 1
}

function install_spacemacs() {
    #git clone spacesmacs
    sudo apt-get install git -y
    git clone https://gitee.com/mirrors/spacemacs.git ~/.emacs.d
    echo "install spacemacs"
}

function modify_elpa_repo() {
    cp ~/.emacs.d/core/templates/.spacemacs.template \
        ~/.emacs.d/core/templates/.spacemacs.template.bak
    cat <<"EOF" | tee ./elpa_repo
(setq configuration-layer--elpa-archives
    '(("melpa-cn" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")
      ("org-cn"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/org/")
      ("gnu-cn"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")))
EOF
    sed -i "/(defun dotspacemacs\/user-init ()/r ./elpa_repo" ~/.emacs.d/core/templates/.spacemacs.template
}


function clone_melpa(){
    mkdir -p ~/.emacs.d/.cache/quelpa/
    git clone https://gitee.com/bolinz/melpa.git ~/.emacs.d/.cache/quelpa/melpa
}

modify_repo
install_emacs
install_spacemacs
modify_elpa_repo
clone_melpa